package com.tjcd.project.library_base.common.utils;

import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;

/**
 * @Author jiangKunKun
 * @CreateDate 2021/7/15
 * @Description 监听多个EditText，只有当它们都有输入后，相应Button才能被点击
 * @SpittingGrooves
 */
public class SomeMonitorEditUtil implements TextWatcher {
    private Button button;
    private EditText[] text;

    public void setMonitorEditText(final Button button, final EditText... text) {
        this.button = button;
        this.text = text;
        for (int i = 0; i < text.length; i++) {
            if (text[i] != null) {
                text[i].addTextChangedListener(SomeMonitorEditUtil.this);
            }
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
        // Auto-generated method stub

    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        // Auto-generated method stub
        for (int i = 0; i < text.length; i++) {
            if (text[i].length() == 0) {
                button.setBackgroundColor(Color.rgb(192, 192, 192));
                button.setEnabled(false);
                return; // 这句代码值两千万
            } else {
                button.setBackgroundColor(Color.rgb(68, 126, 253));
                button.setEnabled(true);

            }
        }
    }
}
